/* 
 * Copyright 2011 VelocitiSoftware
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.velociti.ikarus.ui.widget.demo;

import com.vaadin.Application;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.event.FieldEvents.BlurEvent;
import com.vaadin.event.FieldEvents.BlurListener;
import com.vaadin.event.MouseEvents;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Accordion;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import com.velociti.ikarus.ui.widget.IkarusBTextField;
import com.velociti.ikarus.ui.widget.IkarusBreadCrumb;
import com.velociti.ikarus.ui.widget.IkarusDock;
import com.velociti.ikarus.ui.widget.IkarusDockItem;
import com.velociti.ikarus.ui.widget.IkarusListItem;
import com.velociti.ikarus.ui.widget.IkarusNumericField;
import com.velociti.ikarus.ui.widget.IkarusOptionGroup;
import com.velociti.ikarus.ui.widget.IkarusSlidingPanel;
import com.velociti.ikarus.ui.widget.IkarusTextField;
import com.velociti.ikarus.ui.widget.IkarusUList;
import com.velociti.ikarus.ui.widget.layout.IkarusAbsoluteLayout;

public class IkarusWidgetDemo extends Application {

	public void init() {
		Window mainWindow = new Window("Testproject Application");
		IkarusAbsoluteLayout layout = new IkarusAbsoluteLayout();
		layout.setCaption("IkarusAbsoluteLayout");
		layout.setSizeFull();
		setMainWindow(mainWindow);
		mainWindow.setContent(layout);
		Label label = new Label();
		label.setContentMode(Label.CONTENT_XHTML);
		label.setValue("<b>IkarusAbsoluteLayout:</b> <br> Captions are" + " layout horizontally<br><br>");
		layout.addComponent(label, "top:20px;left:20px;");

		IkarusTextField textField = new IkarusTextField();
		textField.setCaption("Ikarus TextField : ");
		textField.setValue("This is a LETTER only TextField with exceptChars @._#");
		textField.setWidth(310, Sizeable.UNITS_PIXELS);
		textField.setTextType(IkarusTextField.TEXTTYPE_LETTER);
		textField.setExceptionalChars("@._#");
		layout.addComponent(textField, "top:80px;left:150px;");

		IkarusOptionGroup optionGroup = new IkarusOptionGroup("Ikarus OptionGroup1 : ");
		optionGroup.setItems("option1,option2,option3");
		optionGroup.setWidth(200, Sizeable.UNITS_PIXELS);
		optionGroup.select("option2");
		layout.addComponent(optionGroup, "top:110px;left:150px;");

		optionGroup = new IkarusOptionGroup("Ikarus OptionGroup2 : ");
		optionGroup.setMultiSelect(true);
		optionGroup.setItems("option1,option2,option3");
		optionGroup.setWidth(200, Sizeable.UNITS_PIXELS);
		optionGroup.select("option3");
		layout.addComponent(optionGroup, "top:140px;left:150px;");

		IkarusBTextField buttonTextField = new IkarusBTextField("Ikarus BTextField : ");
		buttonTextField.setWidth(500, Sizeable.UNITS_PIXELS);
		buttonTextField.setImmediate(true);
		IndexedContainer ic = new IndexedContainer();
		ic.addItem("xxxx.yyyyyyyyyy@yahoo.com");
		ic.addItem("kkkkkkkk.mmmmm@gmail.com");
		ic.addItem("xxxxxxxxxxx.hhhhhhhhhhh@hotmail.com");
		buttonTextField.setContainerDataSource(ic);
		layout.addComponent(buttonTextField, "top:170px;left:150px;");

		final IkarusNumericField numericField = new IkarusNumericField("IkarusNumericField :");
		numericField.setScaleSize(5);
		numericField.setUseGroupingSeperator(true);
		numericField.setWidth(200, Sizeable.UNITS_PIXELS);
		layout.addComponent(numericField, "top:260px;left:150px;");

		final IkarusNumericField numericField1 = new IkarusNumericField();
		numericField1.setScaleSize(5);
		numericField1.setUseGroupingSeperator(true);
		numericField1.setEnabled(false);
		numericField1.setWidth(200, Sizeable.UNITS_PIXELS);
		layout.addComponent(numericField1, "top:260px;left:365px;");

		numericField.addListener(new BlurListener() {
			public void blur(BlurEvent event) {
				numericField1.setValue(numericField.getValue());
			}
		});

		label = new Label();
		label.setContentMode(Label.CONTENT_XHTML);
		label.setValue("<b>Sample any other layout (VerticalLayout):</b> <br> Captions are layout vertically<br>");
		layout.addComponent(label, "top:330px;left:20px;");

		VerticalLayout vl = new VerticalLayout();
		vl.setSizeFull();
		layout.addComponent(vl, "top:360px;left:150px;");

		buttonTextField = new IkarusBTextField("Ikarus BTextField : ");
		buttonTextField.setImmediate(true);
		buttonTextField.setWidth(500, Sizeable.UNITS_PIXELS);
		vl.addComponent(buttonTextField);

		textField = new IkarusTextField();
		textField.setCaption("This is a DIGIT only TextField with exceptChars .,");
		textField.setValue("123.234.678,98");
		textField.setWidth(150, Sizeable.UNITS_PIXELS);
		textField.setTextType(IkarusTextField.TEXTTYPE_DIGIT);
		textField.setExceptionalChars(".,");
		vl.addComponent(textField);

		optionGroup = new IkarusOptionGroup("Ikarus OptionGroup2 : ");
		optionGroup.setMultiSelect(true);
		optionGroup.setItems("option1,option2,option3");
		optionGroup.setWidth("100%");
		optionGroup.select("option3");
		vl.addComponent(optionGroup);
		addUnorderedList(vl);
		addBreadCrumb(vl);
		addDock(vl);

		addSlidingPanel(mainWindow);
	}

	void addUnorderedList(VerticalLayout vl) {
		IkarusUList ul = new IkarusUList();
		vl.addComponent(ul);
		IkarusListItem li = new IkarusListItem();
		Link l = new Link();
		l.setCaption("Deneme12");
		li.addComponent(l);
		li.setVisible(false);
		ul.addComponent(li);

		li = new IkarusListItem();
		l = new Link();
		l.setCaption("Deneme123");
		li.addComponent(l);
		ul.addComponent(li);
	}

	void addBreadCrumb(VerticalLayout vl) {
		final IkarusBreadCrumb breadCrumb = new IkarusBreadCrumb();
		breadCrumb.setCollapsible(true);
		breadCrumb.setHeight("25px");
		breadCrumb.setWidth("600px");
		Button b = new Button("linkxxxxxxxxxx1");
		breadCrumb.addLink(b);
		Link l = new Link("linkxxxxxxxxxx55", new ExternalResource("http://www.google.com"));
		breadCrumb.addLink(l);

		b = new Button("linkxxxxxxxxxx2");

		breadCrumb.addLink(b);
		b = new Button("linkxxxxxxxxxx3");
		breadCrumb.addLink(b);
		b = new Button("linkxxxxxxxxxx4");
		breadCrumb.addLink(b);
		l = new Link("linkxxxxxxxxxx55", null);
		breadCrumb.addLink(l);
		breadCrumb.setLinkVisible(false, breadCrumb.getIndexofLink(l));
		vl.addComponent(breadCrumb);
		Button button = new Button("Add link to BreadCrumb");
		button.setWidth("250px");
		vl.addComponent(button);
		button.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				Button link = new Button("tttttttttttttttttt");
				breadCrumb.addLink(link);

			}
		});

	}

	void addDock(VerticalLayout layout) {

		HorizontalLayout hl = new HorizontalLayout();
		hl.setSizeFull();

		layout.addComponent(hl);
		layout.setExpandRatio(hl, 1f);

		/* verticalDock */

		IkarusDock dock1 = new IkarusDock("Ikarus Dock style(sample2):");
		dock1.setWidth("160px");

		IkarusDockItem image = null;
		image = new IkarusDockItem("library", new ThemeResource("../dockimages/library.png"));
		dock1.addDockItem(image);
		image = new IkarusDockItem("movies", new ThemeResource("../dockimages/movies.png"));
		dock1.addDockItem(image);
		image = new IkarusDockItem("images", new ThemeResource("../dockimages/images.png"));
		dock1.addDockItem(image);
		image = new IkarusDockItem("tunes", new ThemeResource("../dockimages/tunes.png"));

		dock1.addDockItem(image);
		image = new IkarusDockItem("private", new ThemeResource("../dockimages/private.png"));
		dock1.addDockItem(image);

		dock1.setUseLabel(true);
		dock1.setAlign(IkarusDock.vertical_center);
		dock1.setStyleName("sample2");
		hl.addComponent(dock1);

		/* Horizontal dock */
		final IkarusDock dock = new IkarusDock("Ikarus Dock style(sample1):");
		dock.setUseLabel(true);
		image = new IkarusDockItem("previous", new ThemeResource("../dockimages/previous.png"));
		dock.addDockItem(image);
		image.addListener(new MouseEvents.ClickListener() {
			public void click(com.vaadin.event.MouseEvents.ClickEvent event) {
				// Do something here....
				event.getComponent().getWindow().showNotification("Info : ", (String) ((IkarusDockItem) event.getComponent()).getTitle() + " Pressed");

			}
		});
		image = new IkarusDockItem("pause", new ThemeResource("../dockimages/pause.png"));
		dock.addDockItem(image);
		image.addListener(new MouseEvents.ClickListener() {
			public void click(com.vaadin.event.MouseEvents.ClickEvent event) {
				// Do something here....
				event.getComponent().getWindow().showNotification("Info : ", (String) ((IkarusDockItem) event.getComponent()).getTitle() + " Pressed");

			}
		});
		image = new IkarusDockItem("play", new ThemeResource("../dockimages/play.png"));
		image.setEnabled(false);
		dock.addDockItem(image);
		image.addListener(new MouseEvents.ClickListener() {
			public void click(com.vaadin.event.MouseEvents.ClickEvent event) {
				// Do something here....
				event.getComponent().getWindow().showNotification("Info : ", (String) ((IkarusDockItem) event.getComponent()).getTitle() + " Pressed");

			}
		});
		image = new IkarusDockItem("stop", new ThemeResource("../dockimages/stop.png"));
		dock.addDockItem(image);
		image.addListener(new MouseEvents.ClickListener() {
			public void click(com.vaadin.event.MouseEvents.ClickEvent event) {
				// Do something here....
				event.getComponent().getWindow().showNotification("Info : ", (String) ((IkarusDockItem) event.getComponent()).getTitle() + " Pressed");
				IkarusDockItem image = new IkarusDockItem("play", new ThemeResource("../dockimages/play.png"));
				dock.addDockItem(image);
			}
		});
		image = new IkarusDockItem("next", new ThemeResource("../dockimages/next.png"));
		dock.addDockItem(image);
		image.addListener(new MouseEvents.ClickListener() {
			public void click(com.vaadin.event.MouseEvents.ClickEvent event) {
				// Do something here....
				event.getComponent().getWindow().showNotification("Info : ", (String) ((IkarusDockItem) event.getComponent()).getTitle() + " Pressed");

			}
		});

		dock.setSizeFull();
		dock.setStyleName("sample1");

		hl.addComponent(dock);
		hl.setExpandRatio(dock, 1f);

	}

	void addSlidingPanel(Window mainWindow) {
		IkarusSlidingPanel slidingPanel = new IkarusSlidingPanel();
		slidingPanel.setEdge(IkarusSlidingPanel.EDGE_RIGHT);
		slidingPanel.setHeaderLocation(IkarusSlidingPanel.MIDDLE);
		slidingPanel.setWidth("200px");
		slidingPanel.setHeight("400px");
		slidingPanel.setAnimationEnabled(true);
		slidingPanel.setOpen(false);
		slidingPanel.setContent(getDummyAccordion());
		mainWindow.addComponent(slidingPanel);

		slidingPanel = new IkarusSlidingPanel();
		slidingPanel.setEdge(IkarusSlidingPanel.EDGE_BOTTOM);
		slidingPanel.setHeaderLocation(IkarusSlidingPanel.MIDDLE);
		slidingPanel.setWidth("400px");
		slidingPanel.setHeight("200px");
		slidingPanel.setAnimationEnabled(true);
		slidingPanel.setOpen(false);
		slidingPanel.setContent(getDummyAccordion());
		mainWindow.addComponent(slidingPanel);
	}

	private Accordion getDummyAccordion() {
		final Accordion accordion_1 = new Accordion();
		accordion_1.setImmediate(true);
		accordion_1.setWidth("100%");
		accordion_1.setHeight("100%");

		VerticalLayout verticalLayout_3 = new VerticalLayout();
		verticalLayout_3.setImmediate(false);
		verticalLayout_3.setWidth("100.0%");
		verticalLayout_3.setHeight("100.0%");
		verticalLayout_3.setMargin(false);

		Button b = new Button("Switch to Tab2");
		b.setWidth("100px");
		b.setImmediate(true);

		verticalLayout_3.addComponent(b);
		accordion_1.addTab(verticalLayout_3, "Tab1", null);

		final VerticalLayout verticalLayout_1 = new VerticalLayout();
		verticalLayout_1.setImmediate(false);
		verticalLayout_1.setWidth("100.0%");
		verticalLayout_1.setHeight("100.0%");
		verticalLayout_1.setMargin(false);
		b.addListener(new ClickListener() {
			public void buttonClick(ClickEvent event) {
				accordion_1.setSelectedTab(verticalLayout_1);
			}
		});
		TextField tf = new TextField();
		tf.setWidth("100px");
		verticalLayout_1.addComponent(tf);
		accordion_1.addTab(verticalLayout_1, "Tab2", null);

		return accordion_1;
	}
}
